/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.chipchip.controller;

import com.chipchip.chipchipserver.ZingMeService;
import com.chipchip.chipchipserver.ZingMeUser;
import com.chipchip.database.DBManager;
import com.chipchip.database.objects.DOObstacle;
import io.netty.channel.ChannelHandlerContext;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import zme.api.exception.ZingMeApiException;
import zme.api.graph.ZME_Me;
import zme.api.graph.ZME_User;

/**
 *
 * @author Tri
 */
public class GlobalLogicHandler {
    private static GlobalLogicHandler _instance = null;
    private DBManager db = DBManager.sharedInstance();
    private ZingMeUser zmu = ZingMeUser.sharedInstance();
    private ZingMeService zms = ZingMeService.sharedInstance();
    public ChannelHandlerContext ctx;
    private GlobalLogicHandler() {
        
    }
    
    public static GlobalLogicHandler sharedInstance() {
        if(_instance == null) {
            _instance = new GlobalLogicHandler();
        }
        return _instance;
    }
    
    public void getUserInfoFromSignedRequest(String signedRequest) throws ZingMeApiException, IOException {
        ZME_Me zingMe = zms.getDefaultUser();
        System.out.println("AccessToken: " + zingMe.getAccessToken());
        System.out.println("API KEY: " + zingMe.getApiKey());
        System.out.println("SecretKEY: " + zingMe.getSecretKey());
        System.out.println("AppName: " + zingMe.getAppName());
        ArrayList<String> friendsList = zingMe.getFriends();
        System.out.println("\n\nFriends: \n" + friendsList.toString());
        String fieldToRequest = "id, username, displayname";
        HashMap<String, String> map = zingMe.getInfo(fieldToRequest);
        System.out.println("\nMap:\n" + map.toString());
        
        ZME_User zingUser = new ZME_User(zingMe.getAccessToken());
        System.out.println("Zing USer:\n" + zingUser.getInfo(friendsList, fieldToRequest));
    }
    
    public void testDatabase() {
        DOObstacle testObstacle = new DOObstacle("1", "test", "description", "imageURL");
        db.obstacleTable().insert(testObstacle);
        
       DOObstacle retrieveObstacle = db.obstacleTable().select("1");
       System.out.println("ID: " + retrieveObstacle.getID());
       System.out.println("Name: " + retrieveObstacle.name);
       System.out.println("Desc: " + retrieveObstacle.desc);
       System.out.println("URL: " + retrieveObstacle.imageURL);
    }
}
